﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            Session["UserInfo"] = null;
            return View();
        }

        public ActionResult Ready()
        {
            string classs = Request["class"];
            string name = Request["username"];
            string password = Request["password"];
            string res_herf = "/Login/Index";
            string ready = "NO";

            if(classs=="doctor")
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                try
                {
                    List<Maticsoft.Model.T_Doctor> doctor = bll.GetModelList("(Password='" + password + "'and (Email='" + name + " 'or Tel='" + name + "'))");
                    if (doctor.Count > 0)
                    {
                        ready = "OK";
                        res_herf = "/Doctor/Index";
                        Session["UserInfo"] = doctor[0].Id;
                        Session["UserSelect"] = 0;
                    }
                    else
                    {
                        ready = "NO"; res_herf = "/Login/Index";
                    }
                }
                catch
                {
                    ready = "NO"; res_herf = "/Login/Index";
                }
            }
            else if (classs == "admin")
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                try
                {
                    List<Maticsoft.Model.T_Adm> admin = bll.GetModelList("(Password='" + password + "'and (Email='" + name + " 'or Tel='" + name + "'))");
                    if (admin.Count > 0)
                    {
                        ready = "OK";
                        res_herf = "/Admin/Index";
                        Session["UserInfo"] = admin[0].Id;
                    }
                    else
                    {
                        ready = "NO"; res_herf = "/Login/Index";
                    }
                }
                catch
                {
                    ready = "NO"; res_herf = "/Login/Index";
                }
            }
            else if (classs == "user")
            {
                Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
                try
                {
                    List<Maticsoft.Model.T_User> user = bll.GetModelList("(Password='" + password + "'and (Email='" + name + " 'or Tel='" + name + "'))");
                    if (user.Count > 0)
                    {
                        ready = "OK";
                        res_herf = "/User/Index";
                        Session["UserInfo"] = user[0].Id;
                    }
                    else
                    {
                        ready = "NO"; res_herf = "/Login/Index";
                    }
                }
                catch
                {
                    ready = "NO"; res_herf = "/Login/Index";
                }
            }

            return Content(ready+":"+res_herf);
        }
    }
}
